﻿using System.Linq;
using System.Web.Mvc;
using KaytechSoftware.Cms.App.Areas.Navigation.Models;
using KaytechSoftware.Cms.Services.Interfaces;
using KaytechSoftware.Cms.Services.Interfaces.Entity;

namespace KaytechSoftware.Cms.App.Areas.Navigation.Controllers
{
    public class NavigationController : Controller
    {
        private readonly IPageService _pageService;

        public NavigationController(IPageService pageService)
        {
            _pageService = pageService;
        }

        public PartialViewResult Menu()
        {
            long pageId = 0L;
            if (Request.Url != null && (Request.AppRelativeCurrentExecutionFilePath.Contains("GenericPage")))
            {
                var pageIdString = RouteData.Values["id"] as string;
                if (pageIdString != null)
                    pageId = long.Parse(pageIdString);
            }

            var pages = _pageService.GetAll();
            var model = new MenuModel();
            model.Items = pages.Select(p => new MenuItemModel { Id = p.Id, Label = p.Title, IsSelected = p.Id == pageId }).ToList();
            return PartialView(model);
        }
    }
}